package com.ztest.multithreaddemo1;

import cn.hutool.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class AsyncHttpClientService {


    @Async("taskExecutor")
    public CompletableFuture<Map> sendRequestAsync(Integer batchId) {

        String result = sendRequest();
        Map resultMap = new HashMap();
        resultMap.put("batch",batchId);
        resultMap.put("result",result);
        // 发请求
        return CompletableFuture.completedFuture(resultMap);
    }

    private String sendRequest(){

        String url = "https://wellbase.ylzms.com/jctyxtwx/api/auth/getUserInfo";
        String token = "e09fd80ec4d94f7ba6785150a9b82a48";
        String result = HttpRequest.post(url)
                .header("Content-Type", "application/json")//头信息，多个头信息多次调用此方法即可
                .header("Access-Token",token)
                .timeout(20000)//超时，毫秒
                .execute().body();
        log.debug("返回结果 {}",result);
        return result;
    }
}